#include <stdio.h>

#include "ohos_init.h"
#include "cmsis_os2.h"
#include "iot_gpio_ex.h"
#include "iot_errno.h"
#include "aht20.h"
#include "oled.h"
#include "beep.h"
#include "led.h"
#include "key.h"
#include "mq2.h"
// #include "nfc.h"

static void EnvironmentTask(void)
{
    uint32_t retval = 0;
    float humidity = 0.0f;
    float temperature = 0.0f;
    float luminance = 0.0f;
    double ppm = 0.0;
    static char line[32] = {0};

    Oled_Init();
    Oled_Fill(0);
    Beep_Init();
    Key_Init();
    REL_Init();
    Led_Init();
    Motor_Init();
    // NFCDemoTask();

    for (int i = 0; i < 3; i++)
    {
        snprintf(line, sizeof(line), " %d/%d", (i + 1), 3);
        Oled_ShowChinese(0, 0, 10, 4);
        Oled_ShowChinese(16, 0, 11, 4);
        Oled_ShowChinese(32, 0, 12, 4);
        Oled_ShowChinese(48, 0, 13, 4);
        Oled_ShowString(64, 0, line, 2);

        Beep_On();
        TaskMsleep(500);
        Beep_Off();
        TaskMsleep(500);
    }

    retval = AHT20_Calibrate();
    if (retval != IOT_SUCCESS)
    {
        printf("AHT20 sensor init failed!\r\n");
    }

    while (1)
    {
        AHT20_StartMeasure();

        retval = AHT20_GetMeasureResult(&temperature, &humidity);
        if (retval != IOT_SUCCESS)
        {
            Oled_ShowChinese(0, 0, 0, 4);
            Oled_ShowChinese(16, 0, 2, 4);
            Oled_ShowChinese(32, 0, 20, 4);
            Oled_ShowChinese(48, 0, 21, 4);

            Oled_ShowChinese(0, 2, 1, 4);
            Oled_ShowChinese(16, 2, 2, 4);
            Oled_ShowChinese(32, 2, 20, 4);
            Oled_ShowChinese(48, 2, 21, 4);
        }
        else
        {
            snprintf(line, sizeof(line), ": %.2f C", temperature);
            Oled_ShowChinese(0, 0, 0, 4);
            Oled_ShowChinese(16, 0, 2, 4);
            Oled_ShowString(32, 0, line, 2);

            snprintf(line, sizeof(line), ": %.2f %%", humidity);
            Oled_ShowChinese(0, 2, 1, 4);
            Oled_ShowChinese(16, 2, 2, 4);
            Oled_ShowString(32, 2, line, 2);
        }

        retval = MQ2_GetData(&ppm);
        if (retval != IOT_SUCCESS)
        {
            Oled_ShowChinese(0, 4, 3, 4);
            Oled_ShowChinese(16, 4, 2, 4);
            Oled_ShowChinese(32, 4, 20, 4);
            Oled_ShowChinese(48, 4, 21, 4);
        }
        else
        {
            snprintf(line, sizeof(line), ": %.2lfppm", ppm);
            Oled_ShowChinese(0, 4, 3, 4);
            Oled_ShowChinese(16, 4, 2, 4);
            Oled_ShowString(32, 4, line, 2);
        }

        retval = RES_GetData(&luminance);
        if (retval != IOT_SUCCESS)
        {
            Oled_ShowChinese(0, 6, 8, 4);
            Oled_ShowChinese(16, 6, 9, 4);
            Oled_ShowChinese(32, 6, 20, 4);
            Oled_ShowChinese(48, 6, 21, 4);
        }
        else
        {

            snprintf(line, sizeof(line), ": %.2f lux", luminance);
            Oled_ShowChinese(0, 6, 8, 4);
            Oled_ShowChinese(16, 6, 9, 4);
            Oled_ShowString(32, 6, line, 2);
        }

        if (temperature > 35.0 || temperature < 0 || humidity < 20.0 || humidity > 90.0)
        {
            Beep_On();
            TaskMsleep(500);
            Beep_Off();
        }

        TaskMsleep(500);
    }
}

static void Environment(void)
{
    osThreadAttr_t attr;

    attr.name = "EnvironmentTask";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 1024 * 5;
    attr.priority = osPriorityNormal;

    if (osThreadNew(EnvironmentTask, NULL, &attr) == NULL)
    {
        printf("Environment Falied to create EnvironmentTask!\n");
    }
}
SYS_RUN(Environment);